package com.dmall.algorithm.chapter01;

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

public class Leetcode20V2 {

    public boolean isValid(String s) {
        Map<Character, Character> map = new HashMap<>();
        map.put('(', ')');
        map.put('[', ']');
        map.put('{', '}');
        Stack<Character> stack = new Stack<>();
        for (char c : s.toCharArray()) {
            if (c == '(') {
                stack.push(c);
            } else if (c == '[') {
                stack.push(c);
            } else if (c == '{') {
                stack.push(c);
            } else if (stack.isEmpty() || map.get(stack.pop()) != c) {
                return false;
            }
        }
        return stack.isEmpty();
    }

    public static void main(String[] args) {
        Leetcode20V2 leetcode20 = new Leetcode20V2();
        boolean valid = leetcode20.isValid("()");
        System.out.println(valid);
    }
}
